import { _decorator, Component, Node, sys } from 'cc';
import { lerror, llog, lwarn } from '../../sgame/logger/Logger';
import LocalData, { local_bool, local_clear, local_get, local_num, local_obj, local_remove, local_set } from '../../sgame/localData/LocalData';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    start() {
        this.testLocal();
        local_remove("number");
        llog('local_remove("number")', 'number', local_get("number"));
        local_clear();
        llog("local_clear()", "string", local_get("string"));
        lerror('LocalData.Init(true, "sgame_key", "sgame_iv");');
        LocalData.Init(true, "sgame_key", "sgame_iv");
        this.testLocal();
    }

    testLocal() {
        local_set("null", null);
        local_set("number", 1);
        local_set("string", "string");
        local_set("boolean", true);
        local_set("array", [1, "string", true])
        local_set("object", {
            number: 1,
            string: "string",
            boolean: true,
            array: [1, "string", true],
            object: {
                number: 1,
                string: "string",
                boolean: true,
                array: [1, "string", true],
            }
        });

        const record: Record<string, string> = { key: "value" };
        local_set("record", record);
        // 对Map进行JSON.stringify()会返回一个空对象
        local_set("map", new Map([["key", "value"]]));

        // llog("null", local_get("null"), typeof local_get("null"));
        llog("number", local_num("number"), typeof local_num("number"));
        llog("string", local_get("string"), typeof local_get("string"));
        llog("boolean", local_bool("boolean"), typeof local_bool("boolean"));
        llog("array", local_obj("array"), typeof local_obj("array"));
        llog("object", local_obj("object"), typeof local_obj("object"));
        llog("map", local_obj("map"), typeof local_obj("map"));
        llog("record", local_obj("record"), typeof local_obj("record"));

        lwarn("default number", local_num("inexistence_number", Number.MAX_SAFE_INTEGER), typeof local_num("inexistence_number", Number.MAX_SAFE_INTEGER));
        lwarn("default string", local_get("inexistence_string", "defaultString"), typeof local_get("inexistence_string", "defaultString"));
        lwarn("default boolean", local_bool("inexistence_boolean", true), typeof local_bool("inexistence_boolean", true));
        lwarn("default array", local_obj("inexistence_array", [2, 3, 4]), typeof local_obj("inexistence_array", [2, 3, 4]));
        lwarn("default object", local_obj("inexistence_object", { a: "what" }), typeof local_obj("inexistence_object", { a: "what" }));
        lwarn("default map", local_obj("inexistence_map"), { mapKey: 'mapValue' }, typeof local_obj("inexistence_map"), { mapKey: 'mapValue' });
        lwarn("default record", local_obj("inexistence_record", { recordKey: 'recordValue' }), typeof local_obj("inexistence_record", { recordKey: 'recordValue' }));
    }
}